﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Model;

public partial class News : BasePage
{
    private int pageSize = 20;
    protected int recodCount = 0;
    protected int lastestArticleCount = 0;
    protected int ArticleClassId = 0;
    protected string ArticleClasTitle = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ArticleClassId = StringHelper.isNumber_int(Request["ArticleClassId"]);
            ArticleClasTitle = GetArticleClassName(ArticleClassId);



            ViewState["ArticleClassId"] = ArticleClassId;
            ViewState["ArticleClasTitle"] = ArticleClasTitle;
            BindReapter();
        }
        else
        {
            if (ViewState["ArticleClassId"]!=null)
            {
                ArticleClassId =StringHelper.isNumber_int(ViewState["ArticleClassId"].ToString());
            }
            if (ViewState["ArticleClasTitle"] != null)
            {
                ArticleClasTitle = ViewState["ArticleClasTitle"].ToString();
            }
            
        }
        //StringHelper.stringformat
        BindLaestNewsPic();
    }

    public string GetArticleClassName(int ClassId)
    {
        string className = "";
        string cmdText = "select className from ArticleClass where classId={0}";
        cmdText = string.Format(cmdText, ClassId);
        DataTable dataTable = new DataTable();
        dataTable = ExcuteSqlTextBLL.GetDataTable(cmdText);
        if(dataTable.Rows.Count>0)
        {
            className = dataTable.Rows[0]["ClassName"].ToString();
        }
        return className;
    }


    public void BindReapter()
    {
        //string cmdText = "select *,createname=(select username from users where userid=ar.EditorId ) from Article ar where IsPassed=1 and Isdel=0 and IsMaster=1 and ClassID="+classId+"  order by IsCommend desc,Hits desc";
        string cmdText = "select * from Article ar where ClassID={0}  order by IsCommend desc,Hits desc";
        cmdText = string.Format(cmdText,this.ArticleClassId);
        DataPagesOperate operate = new DataPagesOperate();
        AspNetPager1.PageSize = pageSize;
        operate.BindRepeater(cmdText, AspNetPager1, "IsCommend desc,Hits desc", "ArticleId", rptArticleList, out recodCount);

    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindReapter();
    }

    public void BindLaestNewsPic()
    {
        int count = 5;
        string cmdText = "select top {0} *,CreateName=(select username from Users where Userid=EditorId) from Article where ClassId={1} and DefaultPicUrl<>'images/nopic.jpg' order by ArticleId desc";
        cmdText = string.Format(cmdText,count,this.ArticleClassId);
        DataTable dataTable = new DataTable();
        dataTable=ExcuteSqlTextBLL.GetDataTable(cmdText);
        rptLatestNews.DataSource = dataTable;
        rptLatestNews.DataBind();
        lastestArticleCount = dataTable.Rows.Count;
    }

}
